#coding:utf-8
class EventsController < ApplicationController
  #一覧ß
  def index
    @events = Event.all
    #todo 公開済みで開始が未来のレコードだけを検索する
    respond_to do |format|
      format.html
      format.json {render :json => @events.to_json, :callback => params[:callback]}
      format.xml {render :xml => @events}
    end
  end
  #新規フォームを開く
  def show
    @event = Event.find(params[:id])
    respond_to do |format|
      format.html
      format.json {render :json => @event.to_json, :callback => params[:callback]}
      format.xml {render :xml => @event}
    end
  end
  
  def new
    @event = Event.new
    respond_to do |format|
      format.html
    format.json {render :json => @event.to_json, :callback => params[:callback]}
      format.xml { render :xml => @event}
    end
  end
  #新規登録
  def create
    @event = Event.new(params[:event])
    @event.point = Point.from_x_y(params[:pointx].to_f,params[:pointy].to_f)
    respond_to do |format|
      if @event.update_attributes(params[:event])
        format.html { redirect_to(@event, :notice => 'Event was successfully created !')}
      else
        format.html { render :action => "new"}
      end
    end
  end
  
  #更新フォームを開く
  def edit
    @event = Event.find(params[:id])
    respond_to do |format|
      format.html
      format.xml {render :xml => @event}
    end
  end
  
  #更新
  def update
    @event = Event.find(params[:id])
   @event.point = Point.from_x_y(params[:pointx].to_f,params[:pointy].to_f)
   respond_to do |format|
     if @event.update_attributes(params[:event])
       format.html { redirect_to([@event], :notice => 'Event was successfully created !')}
     else
       format.html { render :action => "edit"}
     end
   end
  end
  
  #削除
  def destroy
    @event = Event.find(params[:id])
    @event.destroy
    respond_to do |format|
      format.html {redirect_to(events_url)}
      format.xml { head:ok}
    end
  end
end
